# 设置目标文件夹路径
$folderPath = "F:\MedAi\pancreas_all"

# 定义要匹配的文件名前缀
$prefixes = @("DWI", "T1_A", "T1_D", "T1_PRE", "T1_V", "T2")

# 遍历所有三级子文件夹
Get-ChildItem -Path $folderPath -Recurse -Directory | Where-Object {
    # 仅处理三级子文件夹
    ($_.FullName -split '\\').Count -eq ($folderPath -split '\\').Count + 3
} | ForEach-Object {
    # 在每个三级子文件夹中查找符合条件的 .nii.gz 文件
    Get-ChildItem -Path $_.FullName -Filter "*.nii.gz" | Where-Object {
        $fileName = $_.Name
        # 检查文件名是否以指定前缀开头
        $prefixes | ForEach-Object { $fileName.StartsWith($_) } | Where-Object { $_ } | Measure-Object | Where-Object { $_.Count -gt 0 }
    } | ForEach-Object {
        # 删除符合条件的文件
        Write-Host "删除文件: $($_.FullName)"
        Remove-Item -Path $_.FullName -Force
    }
}
